package com.uetoken.cn.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.uetoken.cn.R;
import com.uetoken.cn.base.BaseActivity;
import com.uetoken.cn.bean.BlockchainPursesBean;
import com.uetoken.cn.bean.PurseListBean;
import com.uetoken.cn.bean.WithdrawalInfoBean;
import com.uetoken.cn.common.ApiHelper;
import com.uetoken.cn.common.Constant;
import com.uetoken.cn.network.OkHttpUtils;
import com.uetoken.cn.network.Params;
import com.uetoken.cn.network.WebResponse;
import com.uetoken.cn.utils.BlockchainUtils;
import com.uetoken.cn.utils.GlideApp;
import com.uetoken.cn.utils.PhotoUtils;
import com.uetoken.cn.view.DialogHelper;
import com.uetoken.cn.view.popwindow.CurrencySelectPopWindow;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;

import static com.uetoken.cn.common.Constant.EOS_PURSE;
import static com.uetoken.cn.common.Constant.KEY_MENTION_MONEY_ADDRE;
import static com.uetoken.cn.common.Constant.SP_MENTION_MONEY_ADDRE;

public class MentionMoneyActivity extends BaseActivity implements WebResponse, CurrencySelectPopWindow.OnPurseListener {


    @BindView(R.id.topbar)
    QMUITopBar mTopbar;
    @BindView(R.id.goinLayout)
    RelativeLayout goinLayout;
    @BindView(R.id.childPurseIv)
    ImageView childPurseIv;
    @BindView(R.id.childPurseNameTv)
    TextView childPurseNameTv;
    @BindView(R.id.childPurseBalanceTv)
    TextView childPurseBalanceTv;
    @BindView(R.id.mentionMoneyAddressEt)
    AutoCompleteTextView mentionMoneyAddressEt;
    @BindView(R.id.mentionMoneyQuantityEt)
    AppCompatEditText mentionMoneyQuantityEt;
    @BindView(R.id.mentionMoneyMemoEt)
    AppCompatEditText mentionMoneyMemoEt;
    @BindView(R.id.serviceChargeEt)
    TextView serviceChargeEt;
    @BindView(R.id.mentionMoneyBtn)
    Button mentionMoneyBtn;
    @BindView(R.id.monetaryUnitTv)
    TextView monetaryUnitTv;
    @BindView(R.id.serviceChargeMonetaryUnitTv)
    TextView serviceChargeMonetaryUnitTv;
    @BindView(R.id.mentionMoneyMemoLayout)
    LinearLayout mentionMoneyMemoLayout;

    /**
     * 提币网络请求码
     * Blockchain
     */
    private static final int REQUEST_NETWORK_BLOCKCHAIN_CASH_WITHDRAW = 10;
    /**
     * 获取提现信息网络请求码 Withdraw
     * Blockchain
     */
    private static final int REQUEST_NETWORK_BLOCKCHAIN_WITHDRAW_INFO = 11;
    /**
     * 获取区块链钱包网络请求码
     * Blockchain
     */
    private static final int REQUEST_NETWORK_BLOCKCHAIN_PURSE_LIST = 12;

    /**
     * 扫描二维码的请求码
     */
    private static final int INENT_REQUEST_CODE_SCAN = 101;
    /**
     * 用户ID
     */
    private String nodeid;
    /**
     * 钱包序号
     */
    private String mId;
    /**
     * 子钱包id
     */
    private String purseId;
    /**
     * 子钱包名字
     */
    private String purseName;
    /**
     * 子钱包余额
     */
    private String balance = "0";
    /**
     * 钱包类型
     */
    private String purseconfigid;
    /**
     * 订单号
     */
    private String orderid;
    /**
     * 手续费
     */
    private String fee;
    /**
     * memo
     */
    private String memo;
    private int pursetype;//钱包类型
    private String purseaddress;
    private int selPosition;
    private String amount;
    private String minnumber = "0.01";

    private List<BlockchainPursesBean.DataBean> pursesList;
    private CurrencySelectPopWindow mCurrencySelectPopWindow;
    private QMUITipDialog tipDialog;
    private PurseListBean.DataBean mDataBean;
    private String purseunitname;
    private BigDecimal minVal;
    private BigDecimal maxVal;
    private BigDecimal value;
    private Dialog dialog;
    private List<String> purseaddressData = new ArrayList<>();
    private ArrayAdapter adapter;
    private static final String SP_SEPARATOR = ",";
    private static final int MAX_HISTORY_COUNT = 5;// 最多只提示最近的50条历史记录

    @Override
    public int getLayoutId() {
        return R.layout.activity_mention_money;
    }

    @Override
    public void initView() {
        // 沉浸式状态栏
        QMUIStatusBarHelper.translucent(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            QMUIStatusBarHelper.setStatusBarLightMode(this);
        }
        Unbinder unbinder = ButterKnife.bind(this);

        initTopBar();
    }

    @Override
    public void initData() {
        nodeid = SPUtils.getInstance(Constant.UE_TOKEN_PREFERENCE_USER_INFO).getString(Constant.SP_NODE_ID, "");
        Intent intent = getIntent();
        mDataBean = (PurseListBean.DataBean) intent.getSerializableExtra("assetsDetailModel");
        pursetype = mDataBean.getPursetype();
        purseId = mDataBean.getPurseid() + "";
        balance = mDataBean.getBalanceformat();
        purseName = mDataBean.getPursename();
        purseunitname = mDataBean.getPurseunitname();
        minVal = new BigDecimal(minnumber);
        maxVal = new BigDecimal(balance);
        getTransPurses();
        editTextClick();
        initMentionMoneyBtnView();
    }

    @Override
    public void init() {

    }

    private void initTopBar() {
        mTopbar.setTitle(getResources().getString(R.string.str_mention_money));
        mTopbar.addLeftImageButton(R.drawable.ic_back, R.id.qmui_topbar_item_left_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initPursesView() {
        int[] purseInfo = BlockchainUtils.getPurseIcon(purseName);
        int purseIcon = purseInfo[0];
        int newScale = purseInfo[1];
        GlideApp.with(this).load(ContextCompat.getDrawable(this, purseIcon)).into(childPurseIv);
        childPurseBalanceTv.setText(getResources().getString(R.string.str_balance_2) + " " + balance);
        childPurseNameTv.setText(purseName);
        monetaryUnitTv.setText(purseunitname);
        mentionMoneyQuantityEt.setHint(String.format(getString(R.string.str_mention_money_quantity_hint), minnumber));
        serviceChargeMonetaryUnitTv.setText(purseunitname);
        getWithdrawalInfo();
        if (pursetype == EOS_PURSE) {
            mentionMoneyMemoLayout.setVisibility(View.VISIBLE);
        } else {
            mentionMoneyMemoLayout.setVisibility(View.GONE);
        }
        purseaddressData.clear();
        List<String> list = getPurseaddressList();
        if (!ObjectUtils.isEmpty(list)) {
            purseaddressData.addAll(list);
        }
        //创建一个适配器
        if (adapter == null) {
            adapter = new ArrayAdapter<>(MentionMoneyActivity.this, android.R.layout.simple_dropdown_item_1line);
            mentionMoneyAddressEt.setAdapter(adapter);

        }
        addAll(purseaddressData);
    }

    public void addAll(List<String> collection) {
        adapter.clear();
        for (String s : collection) {
            adapter.add(s);
        }

    }

    public void getCheckedPosition(String purseName) {
        for (int i = 0; i < pursesList.size(); i++) {
            BlockchainPursesBean.DataBean desktopBean = pursesList.get(i);
            String name = desktopBean.getPursename();
            if (ObjectUtils.equals(name, purseName)) {
                balance = desktopBean.getBalance() + "";
                purseconfigid = desktopBean.getPurseconfigid() + "";
                minnumber = desktopBean.getMinwithdrawnumber();
                pursetype = desktopBean.getPursetype();
                if (ObjectUtils.isEmpty(minnumber)) {
                    minnumber = "0";
                }
                minVal = new BigDecimal(minnumber);
                maxVal = new BigDecimal(balance);
                selPosition = i;
                return;
            }
        }
    }

    private void initMentionMoneyBtnView() {
//        LogUtils.dTag("initMentionMoneyBtnView", "purseaddress = " + ObjectUtils.isEmpty(purseaddress)
//                + " amount = " + (ObjectUtils.isEmpty(amount) ? true : data1.compareTo(new BigDecimal(amount)) < 0)
//                + " fee = " + ObjectUtils.isEmpty(fee) + " data2 = " + (data1.compareTo(new BigDecimal(amount)) < 0) + " amount = " + amount + " compareTo = " + data1.compareTo(new BigDecimal(amount)));
        int drawable;
        if (ObjectUtils.isEmpty(purseaddress)
                || ObjectUtils.isEmpty(amount)
                || ObjectUtils.isEmpty(fee)
                || (pursetype == EOS_PURSE ? ObjectUtils.isEmpty(memo) : false)) {
            drawable = R.drawable.grey_circular_bead_shape1;
            mentionMoneyBtn.setClickable(false);
        } else {
            drawable = R.drawable.blue_circular_bead_shape_4;
            mentionMoneyBtn.setClickable(true);
        }
        mentionMoneyBtn.setBackground(ContextCompat.getDrawable(MentionMoneyActivity.this, drawable));
    }

    private void operateSuccessfullyDialog() {
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord(getString(R.string.str_operate_successfully))
                .create();
        tipDialog.show();
        mentionMoneyBtn.postDelayed(new Runnable() {
            @Override
            public void run() {
                tipDialog.dismiss();
            }
        }, 1500);
    }

    private void operateFailDialog(String message) {
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                .setTipWord(message)
                .create();
        tipDialog.show();
        mentionMoneyBtn.postDelayed(new Runnable() {
            @Override
            public void run() {
                tipDialog.dismiss();
            }
        }, 1500);
    }

    private void editTextClick() {
        mentionMoneyAddressEt.setOnTouchListener(new View.OnTouchListener() {
            //按住和松开的标识
            int count = 0;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                LogUtils.dTag("mentionMoneyAddressEt", "onTouch = " + event.getAction());
                switch (event.getAction()) {
                    case MotionEvent.ACTION_UP:
                        if (!mentionMoneyAddressEt.isPopupShowing()) {
                            mentionMoneyAddressEt.showDropDown();
                        } else {
                            mentionMoneyAddressEt.dismissDropDown();
                        }
                        break;
                }

                return false;
            }
        });
        mentionMoneyQuantityEt.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(8)});
        mentionMoneyAddressEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 输入前的监听 开始输入
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听 文字变化
                purseaddress = s.toString();

                initMentionMoneyBtnView();
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听 输入结束
                purseaddress = s.toString();
            }
        });

        mentionMoneyQuantityEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 输入前的监听 开始输入
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听 文字变化
                amount = s.toString();

                if (s.length() > 0) {
                    mentionMoneyQuantityEt.setTextSize(24);
                } else {
                    mentionMoneyQuantityEt.setTextSize(14);
                }
                initMentionMoneyBtnView();
                if (!ObjectUtils.isEmpty(amount)) {
                    if (maxVal.compareTo(new BigDecimal(amount)) == -1) {
                        mentionMoneyQuantityEt.setText(balance);
                        mentionMoneyQuantityEt.setSelection((mentionMoneyQuantityEt.getText()).length());
                        ToastUtils.showShort(getString(R.string.str_amount_oversize));
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听 输入结束
                amount = s.toString();
            }
        });

        mentionMoneyMemoEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 输入前的监听 开始输入
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听 文字变化
                memo = s.toString();
                initMentionMoneyBtnView();
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听 输入结束
                memo = s.toString();
            }
        });
    }

    @OnClick({R.id.purseLayout, R.id.QRscanIv, R.id.mentionMoneyBtn, R.id.monetaryAllTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.purseLayout:
                mCurrencySelectPopWindow.showAtLocation(goinLayout, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.QRscanIv:
                //当前手机版本低于23时，用此方法来判断相机是否可用
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                    if (!PhotoUtils.cameraIsCanUse()) {
                        //相机不可用
                        ToastUtils.showShort(getResources().getString(R.string.str_camera_not_use));
                        return;
                    }
                }
                AndPermission.with(this).runtime().permission(Permission.Group.CAMERA).onGranted(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        Intent intent = new Intent(MentionMoneyActivity.this, ScanQrCodeActivity.class);
                        startActivityForResult(intent, INENT_REQUEST_CODE_SCAN);
                    }
                }).onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        ToastUtils.showShort(getResources().getString(R.string.str_camera_not_use));
                    }
                }).start();
                break;
            case R.id.mentionMoneyBtn:
                /**
                 *  nodeid (integer): 用户ID ,
                 *       purseconfigid (integer): 钱包配置id ,
                 *       amount (number): 提币数量 ,
                 *       orderid (string): 订单号 ,
                 *       purseaddress (string, optional): 提币地址 ,
                 */
                purseaddress = mentionMoneyAddressEt.getText().toString();
                amount = mentionMoneyQuantityEt.getText().toString();
                if (ObjectUtils.isEmpty(amount) ? true : minVal.compareTo(new BigDecimal(amount)) > 0) {
                    ToastUtils.showShort(getString(R.string.str_amount_lessthan));
                    return;
                }
                if (ObjectUtils.isEmpty(balance) ? true : maxVal.compareTo(new BigDecimal(amount)) == -1) {
                    ToastUtils.showShort(getString(R.string.str_amount_oversize));
                    return;
                }
                if (ObjectUtils.isEmpty(orderid)) {
                    ToastUtils.showShort(R.string.str_exit_retry_input);
                    return;
                }
                if (ObjectUtils.isEmpty(purseaddress)) {
                    ToastUtils.showShort(R.string.str_withdrawal_address_input);
                    return;
                }
                cashWithdrawal();
                break;
            case R.id.monetaryAllTv:
//                savePurseaddress(purseaddress);
//                purseaddressData.clear();
//                List<String> list = getPurseaddressList();
//                if (!ObjectUtils.isEmpty(list)) {
//                    purseaddressData.addAll(list);
//                }
//                addAll(purseaddressData);
                mentionMoneyQuantityEt.setText(balance);
                break;
        }
    }

    private void showMemoTipDialog() {
        if (dialog == null) {
            dialog = DialogHelper.showTipDialog(this, getString(R.string.str_memo_tip), getString(R.string.str_known), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
        dialog.show();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 扫描二维码/条码回传
        if (requestCode == INENT_REQUEST_CODE_SCAN && resultCode == RESULT_OK) {
            if (data != null) {
                purseaddress = data.getStringExtra("qrCode");
                if (TextUtils.isEmpty(purseaddress)) {
                    ToastUtils.showShort(getResources().getString(R.string.str_no_address));
                    return;
                }
                mentionMoneyAddressEt.setText(purseaddress);
            }
        }
    }

    @Override
    public void onSuccessResponse(Call call, String response, int requestCode) throws
            IOException {
        dissmissDialog();
        LogUtils.dTag("onSuccessResponse==", "onSuccessResponse== response = " + response + " requestCode = " + requestCode);
        switch (requestCode) {
            case REQUEST_NETWORK_BLOCKCHAIN_PURSE_LIST:
                BlockchainPursesBean blockchainPursesListBean = new Gson().fromJson(response, BlockchainPursesBean.class);
                if (blockchainPursesListBean.getResult() > 0) {
                    pursesList = blockchainPursesListBean.getData();
                    getCheckedPosition(purseName);
                    initPursesView();
                    if (mCurrencySelectPopWindow == null) {
                        mCurrencySelectPopWindow = new CurrencySelectPopWindow(MentionMoneyActivity.this, pursesList, selPosition);
                        mCurrencySelectPopWindow.createPopup();
                        mCurrencySelectPopWindow.setAnimationStyle(R.style.dialogSlideAnim);
                        mCurrencySelectPopWindow.setOnPurseListener(this);
                    } else {
                        mCurrencySelectPopWindow.setData(pursesList);
                    }
                } else {
                    ToastUtils.showShort(blockchainPursesListBean.getMessage());
                }
                break;
            case REQUEST_NETWORK_BLOCKCHAIN_WITHDRAW_INFO:
                //{"data":{"orderid":"19113061428","fee":0.00001},"result":1,"message":"成功"}
                WithdrawalInfoBean withdrawalInfoBeanBean = new Gson().fromJson(response, WithdrawalInfoBean.class);
                if (withdrawalInfoBeanBean.getResult() > 0) {
                    WithdrawalInfoBean.DataBean withdrawalInfo = withdrawalInfoBeanBean.getData();
                    orderid = withdrawalInfo.getOrderid();
                    fee = withdrawalInfo.getFee();
                    serviceChargeEt.setText(fee);
                } else {
                    ToastUtils.showShort(withdrawalInfoBeanBean.getMessage());
                }
                break;
            case REQUEST_NETWORK_BLOCKCHAIN_CASH_WITHDRAW:
                try {
                    JSONObject res = new JSONObject(response);
                    int result = res.getInt("result");
                    String message = res.getString("message");
                    if (result > 0) {
                        getTransPurses();
                        operateSuccessfullyDialog();
                        savePurseaddress(purseaddress);
                        purseaddressData.clear();
                        List<String> list = getPurseaddressList();
                        if (!ObjectUtils.isEmpty(list)) {
                            purseaddressData.addAll(list);
                        }
                        addAll(purseaddressData);
                        mentionMoneyAddressEt.setText("");
                        mentionMoneyQuantityEt.setText("");
                    } else {
                        operateFailDialog(message);
                    }
                    getWithdrawalInfo();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    public void onFailResponse(Call call, IOException e, int requestCode) {

    }

    @Override
    public void onSelPurse(BlockchainPursesBean.DataBean pursesBean) {
        mentionMoneyAddressEt.setText("");
        mentionMoneyQuantityEt.setText("");
        purseconfigid = pursesBean.getPurseconfigid() + "";
        purseName = pursesBean.getPursename();
        balance = pursesBean.getBalance() + "";
        purseunitname = pursesBean.getPurseunitname();
        minnumber = pursesBean.getMinwithdrawnumber();
        pursetype = pursesBean.getPursetype();
        if (ObjectUtils.isEmpty(minnumber)) {
            minnumber = "0";
        }
        minVal = new BigDecimal(minnumber);
        maxVal = new BigDecimal(balance);
        initPursesView();
        mCurrencySelectPopWindow.dismiss();
    }

    /**
     * POST /api/Blockchain/GetTransPurses
     * 获取钱包列表
     */
    private void getTransPurses() {
        showLoading();
        OkHttpUtils.getInstance().getStringWithParam(this, ApiHelper.getTransPurses(), REQUEST_NETWORK_BLOCKCHAIN_PURSE_LIST, Params.getTransPursesParams(nodeid));
    }

    /**
     * POST /api/Blockchain/GetWithdrawalInfo
     * 获取提现信息
     */
    private void getWithdrawalInfo() {
        showLoading();
        OkHttpUtils.getInstance().getStringWithParam(this, ApiHelper.getWithdrawalInfo(), REQUEST_NETWORK_BLOCKCHAIN_WITHDRAW_INFO, Params.getWithdrawalInfoParams(nodeid, purseconfigid));
    }

    /**
     * POST /api/Blockchain/CashWithdrawal
     * 提币
     * nodeid (integer): 用户ID ,
     * purseconfigid (integer): 钱包配置id ,
     * amount (number): 提币数量 ,
     * orderid (string): 订单号 ,
     * purseaddress (string, optional): 提币地址 ,
     * memo (string, optional): memo ,
     */
    private void cashWithdrawal() {
        showLoading();
        OkHttpUtils.getInstance().getStringWithParam(this, ApiHelper.getCashWithdrawal(), REQUEST_NETWORK_BLOCKCHAIN_CASH_WITHDRAW
                , Params.getCashWithdrawalParams(nodeid, purseconfigid, amount, orderid, purseaddress, pursetype == EOS_PURSE ? memo : ""));
    }

    public class DecimalDigitsInputFilter implements InputFilter {

        private final int decimalDigits;

        /**
         * Constructor.
         *
         * @param decimalDigits maximum decimal digits
         */
        public DecimalDigitsInputFilter(int decimalDigits) {
            this.decimalDigits = decimalDigits;
        }

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            if (source.equals(".") && dest.toString().length() == 0) {
                return "0.";
            }
            int dotPos = -1;
            int len = dest.length();
            for (int i = 0; i < len; i++) {
                char c = dest.charAt(i);
                if (c == '.' || c == ',') {
                    dotPos = i;
                    break;
                }
            }
            if (dotPos >= 0) {

                // protects against many dots
                if (source.equals(".") || source.equals(",")) {
                    return "";
                }
                // if the text is entered before the dot
                if (dend <= dotPos) {
                    return null;
                }
                if (len - dotPos > decimalDigits) {
                    return "";
                }
            }
            return null;
        }
    }

    /**
     * 取出提币地址列表
     *
     * @return
     */
    private List<String> getPurseaddressList() {
        String data = SPUtils.getInstance(SP_MENTION_MONEY_ADDRE).getString(KEY_MENTION_MONEY_ADDRE + pursetype);
        Gson gson = new Gson();
        Type listType = new TypeToken<List<String>>() {
        }.getType();
        return gson.fromJson(data, listType);
    }

//    private String[] getHistoryArray(String key) {
//        String[] array = SPUtils.getInstance(SP_MENTION_MONEY_ADDRE).getString(KEY_MENTION_MONEY_ADDRE + pursetype).split(SP_SEPARATOR);
//        if (array.length > MAX_HISTORY_COUNT) {         // 最多只提示最近的50条历史记录
//            String[] newArray = new String[MAX_HISTORY_COUNT];
//            System.arraycopy(array, 0, newArray, 0, MAX_HISTORY_COUNT); // 实现数组间的内容复制
//        }
//        return array;
//    }

    /**
     * 保存提币地址
     *
     * @param purseaddress
     */
    private void savePurseaddress(String purseaddress) {
        List<String> datas = getPurseaddressList();
        if (ObjectUtils.isEmpty(datas)) {
            datas = new ArrayList<>();
        }
        if (!datas.contains(purseaddress)) {
            datas.add(purseaddress);
            if (datas.size() > MAX_HISTORY_COUNT) {
                String str = datas.get(0);
                datas.remove(str);
            }
            Gson gson = new Gson();
            String data = gson.toJson(datas);
            SPUtils.getInstance(SP_MENTION_MONEY_ADDRE).put(KEY_MENTION_MONEY_ADDRE + pursetype, data);
        }
    }
}
